﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
namespace SamplesShow
{
    public class TreeItem_Sample : TreeViewItem
    {
        protected UIElement SampleUIElement { get; set; }
        string AssemblyName { get; set; }
        string InstanceName { get; set; }
        public TreeItem_Sample(string assemblyName, string instanceName, string illustrate = "")
        {
            BlankUri =  null;
            IsRemoveAndDestroy = false;
            AssemblyName = assemblyName;
            InstanceName = instanceName;
            Illustrate = illustrate;
            SampleUIElement = null;
            Header = InstanceName;
        }
        public Uri BlankUri { get; set; }
        public string Illustrate { get; set; }
        public virtual UIElement GetUIElement()
        {
            if(SampleUIElement == null)
                SampleUIElement = SamplesCollection.Current.GetClient(AssemblyName, InstanceName);
            return SampleUIElement;
        }
        /// <summary>
        /// 是否为移除的时候并且释放掉，对于这么多的例子集合还是安全一点好
        /// </summary>
        public bool IsRemoveAndDestroy { get; set; }
        public void RemoveSampleUIElement(Panel parent)
        {
            if(SampleUIElement == null)
                return;
            if(parent.Children.Contains(SampleUIElement))
            {
                parent.Children.Remove(SampleUIElement);
                if (IsRemoveAndDestroy)
                {
                    if (SampleUIElement is IDisposable)
                        (SampleUIElement as IDisposable).Dispose();
                    SampleUIElement = null;
                }
            }
        }
    }
}
